package com.controller;

import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Jmtj;
import com.domain.JmtjJlx;
import com.domain.Pomr;
import com.service.JmtjService;
import com.service.PomrService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Controller
public class UserController {
	
	@Autowired
	PomrService pomrService;

	
	@RequestMapping("grxx.do")
	public String grxx(HttpServletRequest request,Model model){
		
		Object o = request.getSession().getAttribute("usercode");
		int jkdah = ObjectUtils.getIntVal(o.toString());
		int uid = pomrService.getUidByJkdah(jkdah);
		
		Pomr pomr = pomrService.getPomrByUid(uid);
		Map<String,List<Object>> pomrCascade = pomrService.getPomraCascade(uid);//没有时间的
		Map<String, List<Map<String, Object>>> pomrCascadeSj = pomrService.getPomrCascadeSJ(uid);//有时间的
		model.addAttribute("pomr", pomr);
		model.addAttribute("mapPomrInfo", pomrService.getPomrMapInfo(uid));
		model.addAttribute("pomrCascade",pomrCascade);
		
		//获得全部的类型表数据
		model.addAttribute("dmZjlx", pomrService.getDmZjlx());
		model.addAttribute("dmHyzk", pomrService.getDmHyzk());
		model.addAttribute("dmZw", pomrService.getDmZw());
		model.addAttribute("dmWhcd", pomrService.getDmWhcd());
		model.addAttribute("dmXx", pomrService.getDmXx());
		model.addAttribute("dmRqlx", pomrService.getDmRqlx());
		model.addAttribute("dmJzlx", pomrService.getDmJzlx());
		
		//获得代码表有的，和没有的
		Map<String,List<Object>> tmpMapDm = new HashMap<String,List<Object>>();//各种代码表
		Map<String,List<Object>> mapDm = new HashMap<String,List<Object>>();
		Map<String,List<Map<String,Object>>> mapDmSj = new HashMap<String,List<Map<String,Object>>>();
		tmpMapDm = pomrService.getDm();
		/*数据转换*/
		//药物过敏史
		List<Object> tmpListYwgms = pomrCascade.get("ywgms");
		if(null!=tmpListYwgms){
			for(int i=0;i<tmpListYwgms.size();i++){
				tmpMapDm.get("ywgms").remove(tmpListYwgms.get(i));
			}
		}
		mapDm.put("ywgms_y", tmpListYwgms);
		mapDm.put("ywgms_n", tmpMapDm.get("ywgms"));

		//暴露史
		List<Object> tmpListBls = pomrCascade.get("bls");
		if(null!=tmpListBls){
			for(int i=0;i<tmpListBls.size();i++){
				tmpMapDm.get("bls").remove(tmpListBls.get(i));
			}
		}
		mapDm.put("bls_y", tmpListBls);
		mapDm.put("bls_n", tmpMapDm.get("bls"));		
		
		//残疾
		List<Object> tmpListCj = pomrCascade.get("cj");
		if(null != tmpListCj ){
			for(int i=0;i<tmpListCj.size();i++){
				tmpMapDm.get("cj").remove(tmpListCj.get(i));
			}
		}
		mapDm.put("cj_y", tmpListCj);
		mapDm.put("cj_n", tmpMapDm.get("cj"));
		
		//燃料类型
		List<Object> tmpListRllx = pomrCascade.get("rllx");
		if(null != tmpListRllx ){
			for(int i=0;i<tmpListRllx.size();i++){
				tmpMapDm.get("rllx").remove(tmpListRllx.get(i));
			}
		}
		mapDm.put("rllx_y", tmpListRllx);
		mapDm.put("rllx_n", tmpMapDm.get("rllx"));
				
		//饮水
		List<Object> tmpListYs = pomrCascade.get("ys");
		if(null != tmpListYs){
			for(int i=0;i<tmpListYs.size();i++){
				tmpMapDm.get("ys").remove(tmpListYs.get(i));
			}
		}
		mapDm.put("ys_y", tmpListYs);
		mapDm.put("ys_n", tmpMapDm.get("ys"));
		
		//厕所
		List<Object> tmpListCs = pomrCascade.get("cs");
		if(null != tmpListCs){
			for(int i=0;i<tmpListCs.size();i++){
				tmpMapDm.get("cs").remove(tmpListCs.get(i));
			}
		}
		mapDm.put("cs_y", tmpListCs);
		mapDm.put("cs_n", tmpMapDm.get("cs"));
		
		//疾病
		List<Map<String,Object>> tmpListJb = pomrCascadeSj.get("jb");
		List<Map<String,Object>> tmpMapDmJb = new ArrayList<Map<String,Object>>();
		if(null != tmpListJb){
			for(int i=0;i<tmpListJb.size();i++){
				tmpMapDm.get("jb").remove(tmpListJb.get(i).get("mc"));
			}
		}
		if(null != tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("jb").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("jb").get(i));
				tmpMap.put("qzsj","");
				
				tmpMapDmJb.add(tmpMap);
			}
		}
		mapDmSj.put("jb_y", tmpListJb);
		mapDmSj.put("jb_n", tmpMapDmJb);		
		
		//手术
		List<Map<String,Object>> tmpListSs = pomrCascadeSj.get("ss");
		List<Map<String,Object>> tmpMapDmSs = new ArrayList<Map<String,Object>>();
		if(null!=tmpListSs){
			for(int i=0;i<tmpListSs.size();i++){
				tmpMapDm.get("ss").remove(tmpListSs.get(i).get("mc"));
			}
		}
		if(null != tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("ss").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("ss").get(i));
				tmpMap.put("sssj","");
				tmpMapDmSs.add(tmpMap);
			}
		}
		mapDmSj.put("ss_y", tmpListSs);
		mapDmSj.put("ss_n", tmpMapDmSs);		
		
		//外伤
		List<Map<String,Object>> tmpListWs = pomrCascadeSj.get("ws");
		List<Map<String,Object>> tmpMapDmWs = new ArrayList<Map<String,Object>>();
		if(null!=tmpListWs){
			for(int i=0;i<tmpListWs.size();i++){
				tmpMapDm.get("ws").remove(tmpListWs.get(i).get("mc"));
			}
		}
		if(null!=tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("ws").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("ws").get(i));
				tmpMap.put("qzsj","");
				tmpMapDmWs.add(tmpMap);
			}
		}
		mapDmSj.put("ws_y", tmpListWs);
		mapDmSj.put("ws_n", tmpMapDmWs);	
		
		model.addAttribute("pomrCascadeSj",mapDmSj);
		
		//输血
		List<Map<String,Object>> tmpListSx = pomrCascadeSj.get("sx");
		List<Map<String,Object>> tmpMapDmSx = new ArrayList<Map<String,Object>>();
		if(null != tmpListSx){
			for(int i=0;i<tmpListSx.size();i++){
				tmpMapDm.get("sx").remove(tmpListSx.get(i).get("mc"));
			}
		}
		if(null!=tmpMapDm){
			//封装为有时间
			for(int i=0;i<tmpMapDm.get("sx").size();i++){
				Map<String,Object> tmpMap = new HashMap<String,Object>();
				tmpMap.put("mc",tmpMapDm.get("sx").get(i));
				tmpMap.put("sxsj","");
				tmpMapDmSx.add(tmpMap);
			}
		}
		mapDmSj.put("sx_y", tmpListSx);
		mapDmSj.put("sx_n", tmpMapDmSx);	
		
		model.addAttribute("mapDm", mapDm);
		model.addAttribute("pomrCascadeSj",mapDmSj);

		
		return "person/grxx";
	}
	


	
	
}
